﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace SurrealTodoAdapter
{
    /// <summary>
    /// 暂时使用一个静态的Cache作为获取用户数据的缓存
    /// 确保获取用户使不会从服务器端反复的获取数据
    /// </summary>
    public class SurrealTodoService
    {
        private SurrealTodoAdapter _surrealTodoAdapter = new SurrealTodoAdapter();

        public static Dictionary<string, User> UserCache = new Dictionary<string, User>();

        public SurrealTodoService()
        {
        }

        public SurrealTodoService(string baseUrl)
        {
            _surrealTodoAdapter = new SurrealTodoAdapter(baseUrl);
        }

        public void AddNewTask(string userName, string taskContent)
        {
            User _user = GetUser(userName);
            TaskList defaultTaskList = _user.GetDefaultTaskList();
            Task task = _surrealTodoAdapter.AddNewTaskInList(defaultTaskList.Id, taskContent, DateTime.Now);
            defaultTaskList.Tasks.Insert(0, task);
        }

        public void AddNewTask(string userName, string taskContent, DateTime taskTiming)
        {
            User _user = GetUser(userName);
            string combineTaskContent = taskContent;

            TaskList defaultTaskList = _user.GetDefaultTaskList();
            Task task = _surrealTodoAdapter.AddNewTaskInList(defaultTaskList.Id, combineTaskContent, taskTiming);
            defaultTaskList.Tasks.Insert(0, task);

            _surrealTodoAdapter.AddTiming(task.id,userName,taskTiming);
        }

        public List<TimingTask> GetTimings()
        {
            return _surrealTodoAdapter.GetTimingTasks();
        }

        public void FinishTask(Task task)
        {
            
        }

        public List<Task> GetToDoTasks(string userName)
        {
            User _user = GetUser(userName);
            return _user.GetDefaultTaskList().Tasks;
        }

        public void FinishAllCurrentTasks(string userName)
        {
            User _user = GetUser(userName);
            List<TaskList> taskLists = _user.GetTaskLists();
            foreach (var task in taskLists[0].Tasks)
            {
                _surrealTodoAdapter.ChangeList(task.id, taskLists[1].Id);
            }

            taskLists[1].Tasks.AddRange(taskLists[0].Tasks);
            taskLists[0].Tasks.Clear();
        }

        public void ClearTask(string userName)
        {
            User _user = GetUser(userName);
            TaskList defaultTaskList = _user.GetDefaultTaskList();
            defaultTaskList
                .Tasks
                .ForEach(task => _surrealTodoAdapter.DeleteTask(task.id));
            defaultTaskList.Tasks.Clear();
        }

        public void ClearUser()
        {
            _surrealTodoAdapter.GetUsers().ForEach(
                    u =>
                    {
                        _surrealTodoAdapter.DeleteUser(u.tab_id);
                    }
                    );
            UserCache.Clear();
        }

        private User GetUser(string userName)
        {
            InitUserCache();
            CheckIfNewUser(userName);

            User cacheUser = UserCache[userName];
            if (cacheUser.Pages == null)
            {
                cacheUser.Pages = _surrealTodoAdapter.GetPages(cacheUser.tab_id);
                var firstPage = cacheUser.Pages.First();
                firstPage.TaskLists = _surrealTodoAdapter.GetTaskLists(firstPage.page_id);
                if (firstPage.TaskLists.Count == 0)
                {
                    TaskList todoList = _surrealTodoAdapter.AddNewList(firstPage.page_id);
                    _surrealTodoAdapter.EditList(todoList.Id, "Todo");

                    TaskList finishedList = _surrealTodoAdapter.AddNewList(firstPage.page_id);
                    _surrealTodoAdapter.EditList(finishedList.Id, "Finished");

                    firstPage.TaskLists = _surrealTodoAdapter.GetTaskLists(firstPage.page_id);
                }
            }
            return cacheUser;
        }

        private void CheckIfNewUser(string userName)
        {
            if (!UserCache.ContainsKey(userName))
            {
                User user = _surrealTodoAdapter.CreateUser(userName);
                UserCache.Add(userName, user);
            }
        }

        private void InitUserCache()
        {
            if (UserCache.Count == 0)
            {
                _surrealTodoAdapter.GetUsers().ForEach(
                    u =>
                    {
                        if (!UserCache.ContainsKey(u.name))
                        {
                            UserCache[u.name] = u;
                        }
                    }
                    );
            }
        }
    }
}